package com.crm.system.controller.trainingManager;

import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.entity.customerManager.Customer;
import com.crm.model.entity.internalTrainingManager.NxContact;
import com.crm.model.entity.trainingManager.Training;
import com.crm.model.entity.trainingManager.TrainingContact;
import com.crm.model.entity.trainingManager.TrainingDemand;
import com.crm.model.entity.trainingManager.TrainingDemandItem;
import com.crm.model.qo.trainingManager.AddTrainingQO;
import com.crm.model.qo.trainingManager.TrainingFormQO;
import com.crm.model.qo.trainingManager.TrainingQO;
import com.crm.model.vo.trainingManager.TrainingContractVO;
import com.crm.model.vo.trainingManager.TrainingDetailVO;
import com.crm.model.vo.trainingManager.TrainingFormVO;
import com.crm.model.vo.trainingManager.TrainingVO;
import com.crm.system.service.trainingManager.TrainingContractService;
import com.crm.system.service.trainingManager.TrainingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 内训管理信息
 */
@RestController
@RequestMapping("/training")
@Slf4j
public class TrainingController {

    @Autowired
    private TrainingService trainingService;

    @Autowired
    private TrainingContractService trainingContractService;

    /**
     * 用户模块 -- 内训管理列表
     *
     * @param pageIndex 页码
     * @param pageSize  分页大小
     * @param sortName  排序列名字
     * @param sortOrder 排序规则(asc/desc)
     * @param trainingQO 查询条件
     * @author baijian
     * @return
     */
    @PostMapping("/trainingList")
    public DataResponse trainingList(HttpServletRequest request,
                                     @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                     @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                     @RequestParam(value = "sortName", required = false) String sortName,//排序列名字
                                     @RequestParam(value = "sortOrder", required = false) String sortOrder,//排序规则(asc/desc)
                                     TrainingQO trainingQO//查询条件
    ) {
        try {
            DhPageInfo<TrainingVO> list = trainingService.selectTrainingList(pageIndex, pageSize, trainingQO, sortName, sortOrder);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), list);
        } catch (Exception e) {
            log.error("training selectTrainingList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 内训管理-新增内训
     * @param training 新增内训实体
     * @author baijian
     * @return
     */
    @PostMapping("/addTraining")
    public DataResponse addTraining(@RequestBody AddTrainingQO training) {
        try{
            String result = trainingService.addTraining(training);
            if("NX0001".equals(result)){
                return new DhResponse(ResponseCodeEnum.TRAINING_EXCEED.getResponseCode());
            }
            if("SUCCESS".equals(result)){
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }catch(Exception e){
            log.error("training addTraining error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 内训管理-新增内训需求
     * @param trainingDemand 新增内训需求实体
     * @author baijian
     * @return
     */
    @PostMapping("/addTrainingDemand")
    public DataResponse addTrainingDemand(@RequestBody TrainingDemand trainingDemand){
        Boolean result = trainingService.addTrainingDemand(trainingDemand);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } else {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 用户模块 -- 内训管理信息
     * @param trainingId 内训线索id
     * @author baijian
     * @return
     */
    @PostMapping("/selectTrainingDemand")
    public DataResponse selectTrainingDemand(@RequestParam(value = "trainingId", required = true) String trainingId
    ) {
        try {
            TrainingDemand trainingDemand = trainingService.selectTrainingDemand(trainingId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), trainingDemand);
        } catch (Exception e) {
            log.error("training selectTrainingDemand error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }


    }

    /**
     * 内训管理-新增内训需求项
     * @param trainingDemandItem 新增内训需求项实体
     * @author baijian
     * @return
     */
    @PostMapping("/addTrainingDemandItem")
    public DataResponse addTrainingDemandItem(@RequestBody TrainingDemandItem trainingDemandItem){

        try{
            String result = trainingService.addTrainingDemandItem(trainingDemandItem);
            if("NXI0001".equals(result)){
                return new DhResponse(ResponseCodeEnum.TRAINING_ITEM_EXCEED.getResponseCode());
            }
            if("SUCCESS".equals(result)){
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }catch(Exception e){
            log.error("training addTrainingDemandItem error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 用户模块 -- 内训管理项列表
     * @param pageIndex 页码
     * @param pageSize  分页大小
     * @param sortName  排序列名字
     * @param sortOrder 排序规则(asc/desc)
     * @param trainingId 内训线索id
     * @author baijian
     * @return
     */
    @PostMapping("/trainingDemandItemList")
    public DataResponse trainingDemandItemList(HttpServletRequest request,
                                     @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                     @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                     @RequestParam(value = "sortName", required = false) String sortName,//排序列名字
                                     @RequestParam(value = "sortOrder", required = false) String sortOrder,//排序规则(asc/desc)
                                     @RequestParam(value = "trainingId", required = true) String trainingId
    ) {
        try {
            DhPageInfo<TrainingDemandItem> list = trainingService.selectTrainingDemandItemList(pageIndex, pageSize, trainingId, sortName, sortOrder);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), list);
        } catch (Exception e) {
            log.error("training trainingDemandItemList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 用户模块 -- 内训联系人信息
     * @param pageIndex 页码
     * @param pageSize  分页大小
     * @param sortName  排序列名字
     * @param sortOrder 排序规则(asc/desc)
     * @param trainingId 内训线索id
     * @author baijian
     * @return
     */
    @PostMapping("/trainingContactList")
    public DataResponse trainingContactList(HttpServletRequest request,
                                               @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                               @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                               @RequestParam(value = "sortName", required = false) String sortName,//排序列名字
                                               @RequestParam(value = "sortOrder", required = false) String sortOrder,//排序规则(asc/desc)
                                               @RequestParam(value = "trainingId", required = true) String trainingId
    ) {
        try {
            DhPageInfo<TrainingContact> list = trainingService.selectTrainingContactList(pageIndex, pageSize, trainingId, sortName, sortOrder);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), list);
        } catch (Exception e) {
            log.error("training trainingContactList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 内训管理-内训详情-新增联系人到内训联系人下
     * @param trainingContact 内训联系人实体
     * @author baijian
     * @return
     */
    @PostMapping("/addTrainingContact")
    public DataResponse addTrainingContact(@RequestBody TrainingContact trainingContact) {
        Boolean result = trainingService.addTrainingContact(trainingContact);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } else {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 内训管理-内训详情-修改联系人到内训联系人下
     * @param trainingContact 内训联系人实体
     * @author baijian
     * @return
     */
    @PostMapping("/updateTrainingContact")
    public DataResponse updateTrainingContact(@RequestBody TrainingContact trainingContact) {
        Boolean result = trainingService.updateTrainingContact(trainingContact);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } else {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     *内训详情-内训联系人 开启/废止联系人
     * @param trainingContact 废止内训联系人实体
     * @author baijian
     * @return
     */
    @PostMapping("/updateAbolishState")
    public DataResponse updateAbolishState(@RequestBody TrainingContact trainingContact) {
        try{
            Boolean result =  trainingService.updateAbolishStateById(trainingContact);
            if(result){
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }catch(Exception e){
            log.error("training updateAbolishState error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 用户模块 -- 内训详情 -- 同一内训线索下内训联系人的联系方式不能相同
     * @param trainingId 内训线索id
     * @param contactInformation 联系方式
     * @author baijian
     * @return
     */
    @PostMapping("/getTrainingIdAndContactInformation")
    public DataResponse getTrainingIdAndContactInformation(HttpServletRequest request,
                                            @RequestParam(value = "trainingId", required = true) String trainingId,//内训id
                                            @RequestParam(value = "contactInformation", required = true) String contactInformation
    ) {
        try {
            TrainingContact trainingContact = trainingService.getTrainingIdAndContactInformation(trainingId, contactInformation);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), trainingContact);
        } catch (Exception e) {
            log.error("training getTrainingIdAndContactInformation error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 用户模块 -- 内训详情 -- 修改联系人回显
     * @param trainingContactId 联系人id
     * @author baijian
     * @return
     */
    @PostMapping("/getTrainingContact")
    public DataResponse getTrainingContact(HttpServletRequest request,
                                                           @RequestParam(value = "trainingContactId", required = true) String trainingContactId//内训id
    ) {
        try {
            TrainingContact trainingContact = trainingService.getTrainingContact(trainingContactId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), trainingContact);
        } catch (Exception e) {
            log.error("training getTrainingIdAndContactInformation error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 管理系统 --内训线索池列表
     * @author yincuimei
     * @return
     */
    @PostMapping("/selectTrainingFormVOList")
    public DataResponse selectTrainingFormVOList(HttpServletRequest request,
                                     @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                     @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                     @RequestParam(value = "sortName", required = false) String sortName,//排序列名字
                                     @RequestParam(value = "sortOrder", required = false) String sortOrder,//排序规则(asc/desc)
                                     TrainingFormQO trainingFormQO//查询条件
    ) {
        try {
            DhPageInfo<TrainingFormVO> list = trainingService.selectTrainingFormVOList(pageIndex,pageSize,trainingFormQO,sortName,sortOrder);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), list);
        } catch (Exception e) {
            log.error("training selectTrainingFormVOList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 管理模块-》内训线索池列表-》内训详情-》编辑内训信息
     * @param trainingId
     * @author yincuimei
     * @return
     */
    @PostMapping("/selectTrainingById")
    public DataResponse selectTrainingById(@RequestParam(value="trainingId",required = false)String trainingId){
        try{
            TrainingDetailVO trainingDetailVO = trainingService.selectTrainingDetailVOById(trainingId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),trainingDetailVO);
        }catch (Exception e){
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 管理模块-》内训线索池列表-》内训详情-》编辑内训
     * @param training
     * @author yincuimei
     * @return
     */
    @PostMapping("/updateTrainingById")
    public DataResponse updateTrainingById(@RequestBody Training training){
        Boolean result = trainingService.updateTrainingById(training);
        if(result){
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        }
        return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
    }

    /**
     * 管理模块-》内训线索池列表-》内训详情-》编辑客户图片
     * @author yincuimei
     * @return
     */
    @PostMapping("/updateCustomerImg")
    public DataResponse updateCustomerImg(Customer customer,@RequestParam MultipartFile file) {
        try{
            Boolean result = trainingService.updateCustomerImg(customer,file);
            if (result) {
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            } else {
                return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
            }
        }catch (Exception e){
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 内训线索池列表(管理)/内训线索(用户)内训详情--合同订单列表
     * @author yincuimei
     * @return
     */
    @PostMapping("/selectTrainingContractListByTrainingId")
    public DataResponse selectTrainingContractListByTrainingId(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                               @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                               @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                               @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                                               String trainingId){//内训ID
        DhPageInfo<TrainingContractVO> trainingContractVOList = trainingContractService.selectTrainingContractListByTrainingId(trainingId,pageIndex,pageSize,sortName,sortOrder);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),trainingContractVOList);
    }


    /**
     * 内训线索池列表(管理)-分配
     * @author yincuimei
     * @return
     */
    @PostMapping("/distribute")
    public DataResponse distribute(String trainingId, String userId){
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), trainingService.distribute(trainingId,userId));
    }

    /**
     * 通过客户id查询内训联系人
     * @author baijian
     * @return
     */
    @PostMapping("/selectContactByCustomerId")
    public DataResponse selectContactByCustomerId(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                               @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                               @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                               @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                                               String customerId){//客户ID
        DhPageInfo<NxContact> trainingContractVOList = trainingContractService.selectContactByCustomerId(customerId,pageIndex,pageSize,sortName,sortOrder);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),trainingContractVOList);
    }
}
